package com.example.myapplication.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.adapter.PrivateOrderAdapter;
import com.example.myapplication.bean.OrderBean;
import com.example.myapplication.bean.User;
import com.example.myapplication.utils.JsonParse;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class SecondFragment extends Fragment {

    private User currentUser; // Placeholder for the current user (you should initialize this with actual user data)
    private PrivateOrderAdapter orderAdapter;

    public SecondFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Initialize the current user (replace this with actual user data)
        currentUser = new User("John Doe", "password123");

        // Initialize the adapter
        orderAdapter = new PrivateOrderAdapter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_second, container, false);

        // Set up UI components
        ImageView avatarImageView = view.findViewById(R.id.iv_user_avatar);
        TextView usernameTextView = view.findViewById(R.id.tv_username);

        // Set values based on the current user
        avatarImageView.setImageResource(R.drawable.default_avatar);
        usernameTextView.setText(currentUser.getUsername());

        // Set up RecyclerView
        RecyclerView recyclerView = view.findViewById(R.id.rv_order_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        recyclerView.setAdapter(orderAdapter);

        // Load JSON data and update the adapter
        loadJsonData();

        return view;
    }

    private void loadJsonData() {
        try {
            // 从 raw 资源文件读取 JSON 数据
            InputStream inputStream = getResources().openRawResource(R.raw.order_list_data);
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();

            // 将缓冲区转换为 JSON 字符串
            String json = new String(buffer, StandardCharsets.UTF_8);

            // 将 JSON 数组解析为 OrderBean 对象的列表
            List<OrderBean> orderList = JsonParse.getInstance().parseOrderList(json);

            // 使用解析后的数据更新适配器
            orderAdapter.setOrderList(orderList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
